import { CustomTableInterface } from '@/components/ProTable/interface';
/**
 * @name 文件上传
 */

import { PublicInterface } from '@/interface/public';
import { useFileForm } from './use-fileForm';
import { useFile } from './use-file';
import { File } from '@/api/interface/file';
import { FileDataProps } from '../index.vue';

export function useFileData(fileRef: PublicInterface.ProTableValue, props: FileDataProps) {
  const { addFileForm } = useFileForm();
  const { fileObj, isImg, onChange, fileBeforeUpload, onSuccess, onExceed } = useFile(addFileForm, fileRef);

  let viewer = ref({
    visible: false,
    pics: ''
  });

  let fileData = reactive<CustomTableInterface>({
    initParam: {
      formType: '',
      formId: ''
    },
    // 表格配置项
    columns: [
      { prop: 'operation', label: 'operation', fixed: 'left', width: 200 },
      {
        prop: 'fileName',
        label: '文件名称'
      },
      {
        prop: 'remark',
        label: 'remark'
      }
    ],
    headButtonList: [
      {
        label: '附件上传',
        type: 'primary',
        icon: 'Plus',
        permissionCode: props.permissionCodeGather?.uploading,
        methods: () => {
          formOpen(addFileForm, '附件上传');
          fileObj.value.fileList = [];
        }
      }
    ],
    rowButtonList: [
      {
        label: '预览',
        type: 'primary',
        hide: row => isImg(row),
        permissionCode: props.permissionCodeGather?.download,
        methods: (row: File.ResFileByForm) => openPreview(row),
        attrs: {
          link: true
        }
      },
      {
        label: '下载',
        type: 'primary',
        permissionCode: props.permissionCodeGather?.download,
        methods: (row: File.ResFileByForm) => {
          const params = { id: row.id, formId: props.formId };
          fileDown(params).then(data => {
            tableExportData(data, row.fileName, true);
          });
        },
        attrs: {
          link: true
        }
      },
      {
        label: '删除',
        type: 'danger',
        permissionCode: props.permissionCodeGather?.delete,
        methods: (row: File.ResFileByForm) => {
          const params = { id: row.id, formId: props.formId };
          useHandleData(
            fileRemove,
            params,
            `${transitionLanguages('table', '删除')}【${row.fileName}】`,
            fileRef.value?.getTableList
          );
        },
        attrs: {
          link: true
        }
      }
    ]
  });

  const openPreview = (row: any) => {
    Object.assign(viewer.value, {
      visible: true,
      pics: `http://file.cnrecyc.com/dfs/${row.filePath}`
    });
    console.log(`http://file.cnrecyc.com/dfs/${row.filePath}`);
  };
  return { fileData, addFileForm, viewer, fileObj, onChange, fileBeforeUpload, onSuccess, onExceed };
}
